// pages/index/index.js
var utils = require('../../utils/utils.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    imgHead:"https://www.304cloud.top:4321"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var time = utils.formatTime(new Date());
    var lunarDate = this.getLunarDate();
    // 再通过setData更改Page()里面的data，动态更新页面的数据
    this.setData({
      time: time.slice(0,10)
    });
    this.getJie();
    this.getDaily();
    wx.login({
      success: function (res) {
        if (res.code) {
          var appid ='wxf5e8b2a51546b5d6'
          var secret ='626b4c1c6f0c9592482619eab41aad35'
          var l = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + `${appid}` + '&secret=' + `${secret}` + '&js_code=' + `${res.code}` + '&grant_type=authorization_code';
          wx.request({
            url: l,
            data: {},
            method: 'GET', 
           success: res =>{
              console.log(res);  //此处获取的数据中及包含openid
              wx.setStorageSync('openid', res.data.openid)
            }
          });
        } else {
          console.log('获取用户登录态失败！' + res.errMsg)
        }
      }
    })
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          console.log("全局--用户已经授权")
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
             console.log(res.userInfo)
             this.setData({
               nickName:res.userInfo.nickName,
               userHead:res.userInfo.avatarUrl
             })
             let openid = wx.getStorageSync('openid')
             this.setData({
               openid:openid
             })
             wx.request({
              url: 'https://www.304cloud.top:4321/getuserinfo',
              header:{
                'content-type':'application/json',
              },
              method:"POST",
              data:{
                user_head:this.data.userHead,
                user_name:this.data.nickName,
                user_self:"你还没有设置噢！",
                user_id:this.data.openid
              },
              success:(res)=>
              {
                console.log(res)
              }
            })
        
            }
          })
        }else{
          console.log("全局--用户还没有授权")
        }
      }
    })
    
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0
      })
    }
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  goToSearch:function(params){
    wx.navigateTo({
      url: '/pages/search/search',
    })
  },
  jumpTo: function (e) {
    console.log(e)
    let option = e.currentTarget.dataset.item;
    this.setData({
      toitem: option
    })
  },
  getLunarDate: function(){
    wx.request({
      url: 'https://api.xlongwei.com/service/datetime/convert.json',
      header:{
        'content-type':'application/json',
      },
      success:(res)=>
      {
        console.log(res)
        this.setData({
          ganzhi:res.data.ganzhi,
          chinese:res.data.chinese.slice(5)
        })
      }
    })
  },
  getJie: function(){
    let jieqi = [];
    let jieri=[];
    
    wx.request({
      url: 'https://www.304cloud.top:4321/getjieqi',
      header:{
        'content-type':'application/json',
      },
      success:(res)=>
      {
        console.log(res.data.data)
        this.setData({
          jieList:res.data.data
        })
        for(let c=0;c<res.data.data.length;c++){
            if(res.data.data[c].jieqi_type=="节气"){
              jieqi.push(res.data.data[c])
            }else{
              jieri.push(res.data.data[c])
            }
          }
        this.setData({
          jieqi:jieqi,
          jieri:jieri
        })
      }
    })
  },
  getDaily:function(e){
    wx.request({
      url: 'https://www.304cloud.top:4321/getdailypoem',
      header:{
        'content-type':'application/json',
      },
      success:(res)=>
      {
        console.log(res.data.data)
        this.setData({
          daily:res.data.data[0],
          dailyPoem:res.data.data[0].poem_txt.replace(/。/g,'\n'),
          dailyHead:"https://www.304cloud.top:4321"
        })
        console.log(this.data.daily)
      }
    })
  },
  goToAll:function(e){
    wx.setStorageSync('jie', e.currentTarget.dataset.value)
    wx.navigateTo({
      url: '/pages/all/all',
    })
  },
  goToDetail:function(e){
    let id=e.currentTarget.dataset.id;
    console.log(id)
    wx.navigateTo({
        url: '/pages/detail/detail?id=' +id
    })
  },
})